##=============================================================================
## Appendix Figure 22
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "plm")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("~/Desktop/clean_ukraine_data.RData")

labs <- c("Survival Mindset (z-std)",
          "Personal Victimization",
          "Family Victimization",
          "VIINA (ADM2)",
          "Frontline Oblast",
          "Displaced",
          "Sex",
          "Ethnic Russian",
          "Age",
          "Education",
          "Employment",
          "Socio-Economic Status")

## Save Lives
fit1 <- plm(save_lives_z ~ survival_mindset_z + per_idx_invcov + fam_idx_invcov +
              viina_cum_adm2_z + frontline_b + displaced + sex + rus_ethnic +
              age + edu + job + finances,
            data = dat, 
            index = c("wave", "adm2_pcode"), 
            model = "within", 
            effect = "twoways")

coeftest(fit1, vcov = vcovHC, type = "HC1")

coefplot::coefplot(fit1, intercept = FALSE, sort = "magnitude") +  
  scale_color_grey() +
  labs(x = "Coefficient Value (z-std)", y = "",
       title = "Two-Way Fixed Effects Model: Save Lives (z-std)") +
  #  scale_y_discrete(labels = labs) +
  theme_bw(base_size = 20) + 
  theme(legend.position = "none") 

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================